package cn.didi.feidi.javabackend.web.waterdeliverapp.controller.exceptionhandler;

import cn.didi.feidi.javabackend.mysql.jpa.exception.MyException;
import cn.didi.feidi.javabackend.mysql.jpa.response.ApiResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by asus on 2017/7/15.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = MyException.class)
    @ResponseBody
    public ApiResponse<String> jsonErrorHandler(HttpServletRequest req, MyException e) throws Exception {
        ApiResponse<String> r = new ApiResponse<String>("1", "MyException:"+e.getMessage());
        e.printStackTrace();
        return r;
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ApiResponse<String> jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ApiResponse<String> r = new ApiResponse<String>("1", "Exception:"+e.toString());
        e.printStackTrace();
        return r;
    }

    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public ApiResponse<String> jsonErrorHandler(HttpServletRequest req, Throwable e) throws Throwable {
        ApiResponse<String> r = new ApiResponse<String>("1", "Exception:"+e.toString());
        e.printStackTrace();
        return r;
    }

}
